package java12;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;


public class BufferedReaderTest {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args)  {
		String FILENAME = "test.txt";
		
		//write using PrintWriter
		long begin = System.currentTimeMillis();
		PrintWriter pw=null;
		try {
			pw = new PrintWriter(FILENAME);
			for(int i = 0;i<10000000;i++){//д��1ǧ����
			pw.println(new Random().nextInt(100000));
		}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally{
			pw.close();
		}
		
		
		long end = System.currentTimeMillis();
		System.out.println("last "+(end-begin));
		System.out.println("write  using PrintWriter done");
		
		//read using Scanner
		begin = System.currentTimeMillis();
		Scanner scanner=null;
		try {
			scanner = new Scanner(new File(FILENAME));
			while(scanner.hasNextLine()){//ֻ�Ƕ���ÿһ�У������κδ���
				scanner.nextLine();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally{
			scanner.close();
		}

		end = System.currentTimeMillis();
		System.out.println("last "+(end-begin));
		System.out.println("read using Scanner done");
		
		//read using BufferedReader
		begin = System.currentTimeMillis();
		BufferedReader br = null;
		try {
			 br = new BufferedReader(new FileReader(new File(FILENAME)));
			while(br.readLine()!=null){};//ֻ�Ƕ������������κδ���
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		end = System.currentTimeMillis();
		System.out.println("last "+(end-begin));
		System.out.println("read using BufferedReader done");
		
		
	}

}
